package com.moba.controller;

import com.alibaba.fastjson.JSONObject;
import com.moba.domain.UserBiRecord;
import com.moba.domain.UserInfo;
import com.moba.domain.UserRelation;
import com.moba.domain.UserRelationPar;
import com.moba.emums.BiGetType;
import com.moba.emums.BiType;
import com.moba.emums.YesOrNo;
import com.moba.service.UserBiRecordService;
import com.moba.service.UserInfoService;
import com.moba.service.UserRelationParService;
import com.moba.service.UserRelationService;
import com.moba.utils.FieldFilter;
import com.moba.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author ding, ning
 * @version 2019-03-06 11:07
 * @date 2019-03-06 11:07
 */
@RestController
@RequestMapping("/userBiRecord")
@Api(value = "")
public class UserBiRecordController extends BaseController {

  @Resource UserBiRecordService userBiRecordService;
  @Resource UserRelationService userRelationService;
  @Resource UserInfoService userInfoService;
  @Resource UserRelationParService userRelationParService;

  @ApiOperation(value = "获取列表")
  @GetMapping(value = "/list")
  public Object list(
      HttpServletRequest request, @ModelAttribute UserBiRecord entity, Integer page) {

    if (null == page) {
      page = 1;
    }

    JSONObject jsonObject = new JSONObject();

    Map<String, Object> paramMap = WebUtils.getParametersStartingWith(request, FieldFilter.PREFIX);
    FieldFilter.setQuery(paramMap, entity);

    entity.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());

    entity.getRow().setCount(10);
    entity.getRow().setFirst((page - 1) * (entity.getRow().getCount()));
    List<UserBiRecord> entityList = userBiRecordService.getEntityPaginatedList(entity);
    if (entityList != null && entityList.size() > 0) {
      for (UserBiRecord temp : entityList) {
        if (temp.getLinkId() != null) {
          UserInfo user = new UserInfo();
          user.setId(temp.getLinkId());
          user = userInfoService.getEntity(user);
          if (user != null) {
            temp.getMap().put("rootUser", user.getUserName());
          }
        }
      }
    }

    jsonObject.put("biTypeList", super.parseEnmuToJSONArray(BiType.class, BiType.values()));
    jsonObject.put(
        "biGetTypeList", super.parseEnmuToJSONArray(BiGetType.class, BiGetType.values()));
    jsonObject.put("dataCount", userBiRecordService.getEntityCount(entity));
    jsonObject.put("entityList", entityList);
    return ResultUtils.success(jsonObject);
  }

  @ApiOperation(value = "获取列表")
  @GetMapping(value = "/appUserBirecordList")
  public Object appUserBirecordList(
      HttpServletRequest request, @ModelAttribute UserBiRecord entity) {

    JSONObject jsonObject = new JSONObject();

    logger.info("entity.getAddUserId():" + entity.getAddUserId());
    entity.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    List<UserBiRecord> entityList = userBiRecordService.getEntityList(entity);
    //    if (entityList != null && entityList.size() > 0) {
    //      for (UserBiRecord temp : entityList) {
    //        if (temp.getLinkId() != null) {
    //          UserInfo user = new UserInfo();
    //          user.setId(temp.getLinkId());
    //          user = userInfoService.getEntity(user);
    //          if (user != null) {
    //            temp.getMap().put("rootUser", user.getUserName());
    //          }
    //        }
    //      }
    //    }

    jsonObject.put("entityList", entityList);
    return ResultUtils.success(jsonObject);
  }

  @ApiOperation(value = "获取列表")
  @GetMapping(value = "/appList")
  public Object appList(
      HttpServletRequest request, @ModelAttribute UserBiRecord entity, Integer page) {
    if (null == page) {
      page = 1;
    }
    JSONObject data = new JSONObject();

    // 个人业绩总和
    BigDecimal userSumMoney = userBiRecordService.getEntityBiSum(entity);
    data.put("userSumMoney", userSumMoney);

    // 直推业绩总和
    UserRelation a = new UserRelation();
    a.setUserParId(entity.getAddUserId());
    BigDecimal userSonSumMoneyLeiji = userRelationService.getUserSonSumMoneyLeiji(a);
    data.put("userSonSumMoneyLeiji", userSonSumMoneyLeiji);

    // 间推业绩总和
    UserRelationPar c = new UserRelationPar();
    c.setUserParId(entity.getAddUserId());
    c.getMap().put("userParLevelLeNe", 1);
    BigDecimal jianTuiSumMoneyLeiji = userRelationParService.getUserSonSumMoneyLeiji(c);
    data.put("jianTuiSumMoneyLeiji", jianTuiSumMoneyLeiji);

    return ResultUtils.success(data);
  }

  @ApiOperation(value = "获取记录列表")
  @GetMapping(value = "/appRecordList")
  public Object appRecordList(
      HttpServletRequest request, @ModelAttribute UserBiRecord entity, Integer page) {
    if (null == page) {
      page = 1;
    }
    JSONObject data = new JSONObject();

    // 个人业绩记录
    logger.info("===个人业绩记录");
    entity.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    List<UserBiRecord> entityList = userBiRecordService.getEntityList(entity);
    data.put("entityList", entityList);

    // 间推业绩
    logger.info("===直推业绩");
    UserRelationPar c = new UserRelationPar();
    c.setUserParId(entity.getAddUserId());
    c.getMap().put("biGetType", BiGetType.BI_GET_TYPE_2.getIndex());
    c.getMap().put("userParLevelLe", 2);
    List<UserRelationPar> zhituiList = userRelationParService.getUserSonBiRecordList(c);
    data.put("zhituiList", zhituiList);

    logger.info("===间推业绩");
    c = new UserRelationPar();
    c.setUserParId(entity.getAddUserId());
    c.getMap().put("biGetType", BiGetType.BI_GET_TYPE_2.getIndex());
    c.getMap().put("userParLevelLeNe", 1);
    List<UserRelationPar> jiantuiList = userRelationParService.getUserSonBiRecordList(c);
    data.put("jiantuiList", jiantuiList);

    return ResultUtils.success(data);
  }
}
